(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	var map = null;
	var toolbar = null;
	var scale = null;
	var editorTool = {};
	var circle = {};
	var pointMarker = {};//轨迹点对象
	var style = {
            color:'#FC740F',
            fillColor:'#FC740F',
            fillOpacity:0.2,
            weight:3
        };
	var styleselect = {
            color:'blue',
            fillColor:'blue',
            fillOpacity:0.2,
            weight:3
        };
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initMap();
			
		});
	});
	function initMap(){
		map = new AMap.Map("map",{
			level:15,//设置地图缩放级别
		});
		map.plugin(["AMap.ToolBar","AMap.OverView","AMap.Scale"],function(){ 
			toolbar = new AMap.ToolBar();
			toolbar.autoPosition=false; //加载工具条 
			map.addControl(toolbar);  	
			scale = new AMap.Scale(); //加载比例尺
			map.addControl(scale);
		});
		map.setDefaultCursor("url(http://developer.amap.com/wp-content/uploads/2014/06/closedhand.cur),pointer");
		$(".amap-logo").hide();
		$(".amap-copyright").hide();
		$(".amap-container").css("position","relative");
//		$(".amap-container").css("position","absolute");
		initSchool();
	}
	function initSchool(){
		var userKey = $("#user-name",parent.parent.document).attr("key");		
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var data = {
				cmd:"getSchoolByPage",
				model:"school",
				userKey:userKey,
				data:{
					currentpage:1,
					pagesize:10,
					schoolcode:schoolcode
				},
				remark:"",
				
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var data = res.data.data[0];
				for(var key in data){
					if(data[key] == null || data[key] == "null"){
						data[key] = "";
					}
				}
				$("#school-name").val(data.name);
				$("#school-schoolcode").val(data.schoolcode);
				$("#school-center").val(data.center);
				$("#school-remark").text(data.remark);
				$("#school-adress").text(data.adress);
				var x = data.center.split(",")[0];
				var y = data.center.split(",")[1];
				var fenceradius = data.radius;
				var schoolcode = data.schoolcode;
			    addCircle(schoolcode,new AMap.LngLat(x,y),fenceradius*100,style,data);
			    var marker = new AMap.Marker({
			    	position: new AMap.LngLat(x,y),
			    });
			    marker.setMap(map);
			    map.panTo(new AMap.LngLat(x,y));
				
			}
		},null,null);	
	}
	function addCircle(name,latlng,radius,options,attr){
		if(circle[name]){
			circle[name].setMap(null);
			circle[name] = null;
		}
		circle[name] = new AMap.Circle({
			id:name,
			center:latlng,//圆心，基点
			radius:radius,//半径
			strokeColor: options.color,//线颜色
			strokeOpacity: 1,//线透明度
			strokeWeight: 3,//线宽
			fillColor: options.fillColor,//填充颜色
			fillOpacity: 0.2//填充透明度
		});
		circle[name].setExtData({attr:attr});
		circle[name].setMap(map);
	}
    function updateSchoolFence(){
    	var userKey = $("#user-name",parent.parent.document).attr("key");
    	var obj = {};
    	obj.schoolcode = $("#school-schoolcode").val();
    	obj.name = encodeURI($("#school-name").val());
    	obj.center = $("#school-center").val();
    	obj.remark = encodeURI($("#school-remark").val());
    	obj.adress = encodeURI($("#school-adress").val());
    	//obj.fence = $("#school-fence").val();
    	var data = {
				cmd:"updateSchoolInfo",
				model:"school",
				userKey:userKey,
				data:obj,
				remark:""
				
		};
		processBegin("正在保存···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("修改学校信息成功");
				initSchool();
			}
		},null,null);
    }
    function pagebtns(){
		$("#save-school-btn").click(function(){
			updateSchoolFence();
		});
	}
    function checkSave(){
    	if($("#school-name").val() == ""){
    		$("#school-name").focus();
    		$("#school-name-ex").html("不能为空");
    		return false; 
    	}else if($("#school-adress").val() == ""){
    		$("#school-adress").focus();
    		$("#school-adress-ex").html("不能为空");
    		return false; 
    	}else if($("#school-remark").val() == ""){
    		$("#school-remark").focus();
    		$("#school-remark-ex").html("不能为空");
    		return false; 
    	}else{
    		return true;
    	}
    }
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();